﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.Entity.AutoGenerator;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;

namespace Xinkui.WebModel.Cache {
    public class ProvinceCache {
        public static string GetENName(int countryId, int provinceId) {
            var entity = Get(countryId, provinceId);
            if (entity != null) { return entity.Province_EN; }
            else { return string.Empty; }
        }

        public static CommonProvince_Entity Get(int countryId, int provinceId) {
            var countryList = ListByCountry(countryId);
            if (countryList != null) {
                return countryList.FirstOrDefault(u => u.ProvinceID == provinceId);
            }
            return null;
        }

        public static List<CommonProvince_Entity> ListByCountry(int countryId) {
            var dict = Dict();
            List<CommonProvince_Entity> list = null;
            if (dict != null) {
                dict.TryGetValue(countryId, out list);
            }
            return list;
        }

        public static Dictionary<int, List<CommonProvince_Entity>> Dict() {
            return CacheProxy.Get<Dictionary<int, List<CommonProvince_Entity>>>(SystemConst.CacheConst.GetProvinceDictKey(), DictFromDb);
        }


        public static Dictionary<int, List<CommonProvince_Entity>> DictFromDb() {
            List<CommonProvince_Entity> datas = Singleton<CommonProvinceDataAccess>.Instance.CommonProvince_All();
            var groupList = datas.GroupBy(u => u.CountryID);
            return groupList.ToDictionary(u => u.Key, u => u.Select(t => t).ToList());
        }


    }
}
